﻿
@{
    Layout = null;
}

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="~/Areas/Back/lib/layui/layui.js"></script>
    <link href="~/Areas/Back/lib/layui/css/layui.css" rel="stylesheet" />
</head>

<body>
    <form class="layui-form" onsubmit="return false" style="margin-top: 50px;">
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-inline">
                <input type="text" id="title" class="layui-input" />
            </div>
            <label class="layui-form-label">分类</label>
            <div class="layui-input-inline">
                <select id="TypeId">
                    <option value="">全部</option>               
                    @foreach (var item in ViewBag.A)
                    {
                        <option value="@item.Id">@item.Name</option>
                    }                   
                </select>
            </div>
            <label class="layui-form-label">是否显示在首页</label>
            <div class="layui-input-inline">
                <select id="Flag">
                    <option value="">全部</option>
                    <option value="是">是</option>
                    <option value="否">否</option>
                </select>
            </div>
            <button id="search" data-type="reload" class="layui-btn">搜索</button>
        </div>
    </form>
    <table id="my_table" lay-filter="test"></table>
    <script type="text/html" id="top_tool">

        <button class="layui-btn" lay-event="bbb">新建新闻</button>
    </script>
    <script type="text/html" id="inline_tool">
        <button class="layui-btn layui-btn-sm layui-icon layui-icon-edit" lay-event="ccc">编辑</button>
        <button class="layui-btn layui-btn-sm layui-icon layui-icon-delete layui-btn-danger" lay-event="ddd">删除</button>
    </script>
    </div>
</body>
<script src="~/Areas/Back/js/Date.js"></script>
<script>
    function dateToStr(date) {
        date.replace(/Date\([\d+]+\)/, function (a) { eval('d = new ' + a) });
        return d.format("yyyy-MM-dd hh:mm:ss");
    }
    layui.use(['table', 'form'], function () {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.$;
        table.render({
            elem: "#my_table",
            toolbar: "#top_tool",
            url: "DelLogin",
            page: true,
            method: "post",
            cols: [[{
                type: 'checkbox'
            }, {
                field: 'Title',
                title: '标题',
            }, {
                field: 'Time',
                title: '创建时间',
                templet: function (d) {
                    return dateToStr(d.Time)
                }
            }, {          
                field: 'Cont',
                title: '新闻详情',
            }, {
                field: 'TypeName',
                title: '所属类别',
            }, {
                field: 'HomeFlag',
                title: '是否显示在首页',
            }, {
                fixed: 'right',
                title: '操作',
                toolbar: '#inline_tool'
            }]]
        });
        var active = {
            
            reload: function () {
                table.reload('my_table', {
                    where: {
                        title: $("#title").val(),
                        isPub: $("#Flag").val(),
                        type: $("#TypeId").val(),
                    }
                });
            }
        }
        $("#search").click(function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        table.on('toolbar(test)', function (obj) {
            if (obj.event == "bbb") {
                layer.open({
                    type: 2,
                    area: ['600px', '600px'],
                    content: "/Back/News/Add",
                })


            }
        });
        table.on('tool(test)', function (obj) {
            var data = obj.data; 
            if (obj.event == "ccc") {
                layer.open({
                    type: 2,
                    area: ['600px', '600px'],
                    content: "Edit?Id=" + data.Id
                })
            } else if (obj.event == "ddd") {
                layer.confirm("真的删除吗？", function (index) {
              
                    $.ajax({
                        url: "DelArt",
                        type: "post",
                        dataType: "json",
                        data: {
                            Id: data.Id
                        },
                        success: function (res) {
                      
                            if (res.success == true) {
                                
                                layer.close(index);
                    
                                location.reload();
                            } else {
                                layer.msg(res.msg);
                            }

                        },
                        error: function (e) {
                            console.log(e);
                        }
                    });
                });
            }
        });
    });
</script>

</html>